/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */




package learn.qual;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.Scanner;

/**
 *
 * @author robit
 */
public class ThemePark1 {
    /**
    * calculate the answer
    */

    static long reckon(int round, int seats, int[] groups) {
        String roundB3=Integer.toString(round, 3);
        long cost[][] = new long[roundB3.length()][groups.length];
        int next[][] = new int[roundB3.length()][groups.length];
        for(int i=0;i<groups.length;i++){
           int nextIdx=i;
           for(int j=0;j<groups.length;j++){
               if(groups[(i+j)%groups.length]+cost[0][i]<=seats){
                   cost[0][i]+=groups[(i+j)%groups.length];
                   nextIdx=(i+j+1)%groups.length;
               }else break;
           }
           next[0][i]=nextIdx;
        }
        for(int i=1;i<roundB3.length();i++){
            for(int j=0;j<groups.length;j++){
                long temp=0;int index =j;
                temp=cost[i-1][index];
                index=next[i-1][index];
                temp+=cost[i-1][index];
                index=next[i-1][index];
                temp+=cost[i-1][index];
                cost[i][j]=temp;
                next[i][j]=next[i-1][index];
            }
        }
        int index=0;
        long totalCost=0;
        for(int i=roundB3.length();i>0;i--){
            
            switch(roundB3.charAt(roundB3.length()-i)){
                case '1':
                    totalCost+=cost[i-1][index];
                    index = next[i-1][index];
                    //System.out.println(cost[i-1][index]);
                    break;
                case '2':
                    totalCost+=cost[i-1][index];
                    index = next[i-1][index];
                    totalCost+=cost[i-1][index];
                    index = next[i-1][index];
                    break;
            }
        }
        return totalCost;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        Scanner s = null;
        PrintWriter o = null;
        try {
            s = new Scanner(
                    new BufferedReader(new FileReader("C-large-practice.in")));
            //s.useLocale(Locale.US);
            o = new PrintWriter(
                    new BufferedWriter(new FileWriter("C-large-practice.out")));

            // Determine input line number
            int lineNums = s.nextInt();

            for (int i = 1; i <= lineNums; i++) {

                // Determine arguments number assign const if fixed
                int Round = s.nextInt();
                int seats = s.nextInt();
                int groupNums = s.nextInt();
                //int argNums = 2;

                // Read all inputs for a line
                //BigInteger argsSet[] = new BigInteger[argNums];
                int groupsSet[] = new int[groupNums];

                // Process a line
                for (int j = 0; j < groupNums; j++) {
                    //groupsSet[j] = s.nextBigInteger();
                    groupsSet[j] = s.nextInt();
                }

                // Calculate answer for a line
                long answer = reckon(Round, seats, groupsSet);
                //int answer = reckon(argsSet);
                o.println("Case #" + i + ": " + answer);
            }
        } finally {
            s.close();
            o.close();
        }
    }
}
